import requests
import logging
from parsel import Selector
import json

"""
 爬取站点:  https://savegram.app/en
"""
base_url = 'https://savegram.app/en'

# 代理
proxies = {
    'http': '127.0.0.1:7890',
    'https': '127.0.0.1:7890'
}
# 定义日志
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s: %(message)s')


def api_ajax(link_url, method='POST'):
    """
    根据目标站点链接，站点名称，返回解析的资源
    :param link_url: 站点链接
    :param method: 请求方式 默认POST
    :return:
    """
    data = {
        'k_exp': '1748716734',
        'k_token': '1a0bfa580f8dc95a2847809f980d4779b3056f144cfe19a0a3a6842e03155618',
        'q': link_url,
        't': 'media',
        'lang': 'en',
        'v': 'v2'
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'
    }
    result = None
    logging.info('开始请求url, url: %s', link_url)
    try:
        # 发出请求
        response = requests.request('POST', url='https://savegram.app/api/ajaxSearch', data=data, headers=headers,
                                    proxies=proxies)
        # 响应成功
        if response.status_code == 200:
            result = response.json()
            return result
        logging.error('请求状态码错误, code: %s', response.status_code)
    except requests.RequestException as e:
        logging.error('请求链接报错，url: %s', link_url, exc_info=True)


def get_html(data):
    """
    处理之后返回正确的html
    :param data:
    :return:
    """
    # 先移除 \r\n
    cleaned_html = data.replace('\r\n', '\n')
    # 如有其它转义继续替换
    cleaned_html = cleaned_html.replace('\\"', '"').replace("\\'", "'")
    # 返回处理后正确的html
    return cleaned_html


# todo 通过html获取所有资源以及封面、评论
def get_all_item(html):
    pass


if __name__ == '__main__':
    # res = api_ajax(link_url='https://www.instagram.com/p/DFW9hH7qF9C/')
    res = api_ajax(link_url='https://www.instagram.com/p/DIYXbXmSKlz/?igsh=OXp1eTZ2OXNpeTFq')
    data = res.get('data')
    html = get_html(data)
    # print(html)
    selector = Selector(text=html)
    items = selector.css('.download-items')
    # 遍历items，提取封面src、资源下载链接
    result = []
    for item in items:
        item_data = {
            'is_video': False,
            'thumb_link': '',
            'resource_link': ''
        }
        # 提取封面链接
        thumb_img_res = item.css('.download-items__thumb>img::attr("data-src")')
        if thumb_img_res:
            thumb_link = thumb_img_res.get()
            item_data['thumb_link'] = thumb_link
        # 提取资源链接，最后一个div.download-items__btn
        resource_res = item.css('.download-items__btn:last-child>a::attr("href")')
        if resource_res:
            resource_res_link = resource_res.get()
            item_data['resource_link'] = resource_res_link
            # 提取title属性判断 是否是video
            video_type_res = item.css('.download-items__btn:last-child>a::attr("title")')
            if video_type_res:
                is_video = True if 'Video' in video_type_res.get() else False
                item_data['is_video'] = is_video
        # 如果是非视频，重写取一下封面链接
        if item_data['is_video']:
            thumb_img_res = item.css('.download-items__thumb>img::attr("src")')
            if thumb_img_res:
                thumb_link = thumb_img_res.get()
                item_data['thumb_link'] = thumb_link

        result.append(item_data)

    # 结果集转换为json
    json_str = json.dumps(result, ensure_ascii=False, indent=2)
    # print(len(items))
    print(json_str)
    # print(html)
